<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/22
 * Time: 10:12
 */

namespace App\Admin\Controllers\Banner;

use App\Models\Banner;
use App\Models\Region;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;

class BannerController extends AdminController
{
    public $title = '广告管理';

    /**
     * 列表
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Banner(), function (Grid $grid) {
            $grid->id;
            $grid->column('name', '名称');
            $grid->path('图片')->image();
            $grid->column('city_name', '城市名称');
            $grid->column('type', '是否全国显示')->display(function ($receiver_type) {
                return Banner::RECEIVER_TYPE[$receiver_type];
            });
            $grid->column('created_at', '创建时间');
            $grid->column('updated_at', '更新时间');
            $grid->actions(function ($action) {
                // $action->disableRemove();
            });
        });
    }


    /**
     * 表单
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Banner());
        $form->text('name', '名称')->rules('required');
        $form->image('path', '广告图')->rules('required')->disableRemove()->accept('jpg,png,gif,jpeg')->disk('oss');
        $form->select('type', '是否全国显示')->placeholder('red')->default(1)->options(Banner::RECEIVER_TYPE)->rules('required');
        $form->select('province_id', '省份')->rules('required')
            ->options(Region::selectOptionByPid(0))
            ->load('city_id', '/tool/region/getCity');
        $form->hidden('province_name')->saving(function () use ($form) {
            return Region::query()->find($form->province_id) ? Region::query()->find($form->province_id)->name : '';
        });

        $form->select('city_id', '城市')->rules('required')
            ->options(function ($city_id) use ($form) {
                $options = [];
                if ($city_id)
                    $options = Region::selectOptionByPid($form->model()->province_id);

                return $options;
            });

        $form->hidden('city_name')->saving(function () use ($form) {
            return Region::query()->find($form->city_id) ? Region::query()->find($form->city_id)->name : '';
        });
        $form->url('url', '跳转地址')->default('https://www.baidu.com');
        return $form;
    }

    /**
     * 详情
     * @param $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show($id, new Banner());
        $show->id;
        $show->field('name', '名称');
        $show->path('图片')->image();
        $show->field('province_name', '省份');
        $show->field('city_name', '城市');
        $show->field('type', '是否全国显示')->as(function ($receiver_type) {
            return Banner::RECEIVER_TYPE[$receiver_type];
        });
        $show->field('url', '跳转地址');
        $show->disableEditButton();

        return $show;
    }

    /**
     * @param int $id
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     * 删除
     */
    public function destroy($id)
    {

        $re_del_message = Banner::destroy($id);
        if (!$re_del_message) {
            return response()->json(['status' => false, 'message' => '服务器繁忙！']);
        }
        return response()->json(['status' => true, 'message' => '删除成功']);

    }
}